package promogui;

import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;


public class DeltaInputTableModel extends AbstractTableModel implements IInputTableModel
{
    private Project project;
    private Engineer engineer;
    private RowHeaderListModel headerModel;
    
    public DeltaInputTableModel(Project project, Engineer engineer)
    {
        this.project = project;
        this.engineer = engineer;
        
        headerModel = new RowHeaderListModel(getRowCount());
    }
    
    public RowHeaderListModel getRowHeaderModel()
    {
        return headerModel;
    }
    
    @Override
    public int getRowCount() 
    {
        return project.getCurrentWeekOfEngineer(engineer);
    }

    @Override
    public int getColumnCount() 
    {
        return 5;
    }

    public void insertWeek()
    {
        int week = project.getCurrentWeekOfEngineer(engineer);
        
        if (week + 1 <= project.getNumberOfPlannedWeeks())
        {
            project.setWeeklyEngineerData(week + 1, new WeeklyEngineerData(engineer));
            fireTableRowsInserted(week + 1, week + 1);
            headerModel.insertRow();
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Sie können keine neue Woche einfügen,\n da das Projekt bereits"
                    + " beendet ist.", "Projekt beendet", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
    @Override
    public boolean isCellEditable(int row, int col) {return isEditabe();}
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) 
    {
        /*
        if ( rowIndex + 1 > project.getCurrentWeek() )
        {
           return new Float(0.0);
        }
         */
        
        WeeklyEngineerData data = project.getTotalWeeklyEngineerData(rowIndex + 1, engineer);
        
        switch (columnIndex)
        {
            case 0: return new Float(data.RE);
            case 1: return new Float(data.AD);
            case 2: return new Float(data.II);
            case 3: return new Float(data.BT);
            case 4: return new Float(data.PM);
        }
        
        return null;
    }
    
    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex)
    {
        
        try
        {

            int week = rowIndex + 1;

            WeeklyEngineerData data = project.getTotalWeeklyEngineerData(week, engineer);

            float effort = Float.parseFloat( (String) value);
            
            if (effort >= 0)
            {

                switch (columnIndex)
                {
                    case 0: data.RE = effort; break;
                    case 1: data.AD = effort; break;
                    case 2: data.II = effort; break;
                    case 3: data.BT = effort; break;
                    case 4: data.PM = effort; break;
                }

                project.setWeeklyEngineerData(week, data);
            }
        }
        
        catch (NumberFormatException e)
        {
            
        }
        
    }
    
    public String getColumnName(int column)
    {
        switch (column)
        {
            case 0: return "Requirement Engineering";
            case 1: return "Architecture and Design";
            case 2: return "Implementation and Integration";
            case 3: return "Build and Testing";
            case 4: return "Project Management";
        }
        
        return "";
    }
    
    public void removeLastWeek()
    {
        int week = getRowCount();
        if ( project.removeCurrentEngineerData(engineer) )
        {
            fireTableRowsDeleted(week, week);
            headerModel.removeRow();
        }
    }

    @Override
    public boolean isEditabe() 
    {
        return true;
    }
    
}
